package 刷题7月.链表反转递归;//0707
//：递归：以相似的方法重复，类似于树结构，先从根节点找到叶子节点，从叶子节点开始遍历
//大的问题(整个链表反转)拆成性质相同的小问题(两个元素反转)curr.next.next = curr
//将所有的小问题解决，大问题即解决
//只需每个元素都执行curr.next.next = curr，curr.next = null两个步骤即可
//        为了保证链不断，必须从最后一个元素开始
public class ReverseList {
    static class ListNode{
        int val;
        ListNode next;
        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
    public static ListNode recursion(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode newHead = recursion(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }

    public static void main(String[] args) {
        ListNode node5 = new ListNode(5,null);
        ListNode node4 = new ListNode(4,node5);
        ListNode node3 = new ListNode(3,node4);
        ListNode node2 = new ListNode(2,node3);
        ListNode node1 = new ListNode(1,node2);
        ListNode node = recursion(node1);
        //可以使用debug方式查看每个结点的值及其指针
        System.out.println(node);
    }
}
